﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication {
    public partial class OperatorMicrosection {
        partial void ReassignBox_Execute() {
            string ToMsBoxNo = "";
            MsBox ToMsBox = null;

            string Message = "To move Microsection: " + this.MsBags.SelectedItem.MsNo + Environment.NewLine;
            Message += "Enter a new box No. (e.g. A005-13)";
            ToMsBoxNo = this.ShowInputBox(Message, "Re-assign Box", "");
            if (ToMsBoxNo == null) { return; } // user clicked cancel button.
            if (IsMsBoxNoValid(ToMsBoxNo, out ToMsBox) == true) {
                this.MsBags.SelectedItem.MsBox = ToMsBox;
                this.Save();
                this.Refresh();
            }
            else {
                Message = "The entered Box number is invalid, please check and enter again.";
                this.ShowMessageBox(Message, "Error Message", MessageBoxOption.Ok);
            }
        }

        bool IsMsBoxNoValid(string ToMsBoxNo, out MsBox ToMsBox) {
            bool result = false; ToMsBox = null;
            if ((ToMsBoxNo != null) && ToMsBoxNo.Length > 0) {
                ToMsBox = this.DataWorkspace.FtgQcData.MsBoxes.Where(i => i.BoxNo == ToMsBoxNo.ToUpper()).FirstOrDefault();
                if (ToMsBox != null) {
                    result = true;
                }
            }
            return result;
        }        
    }
}
